package com.example.neartweet;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neartweet.TwitterApp.TwDialogListener;

public class DetailsActivity extends Activity {

	private String username;
	private String tweet;
	private Double lat;
	private Double lon;
	private ImageButton reply;
	private ImageButton retweet;
	private ImageButton flag;
	private ImageButton map;
	private boolean location;
	private TwitterApp mTwitter;
	private String userTwitter = "";
	
	private static final String twitter_consumer_key = "9bO9i0f0urTGfSx4LqNJA";
	private static final String twitter_secret_key = "nnBmT2sLwnsMbPMTdoWuOzRJRCJHPIfYdiIrub0DKQ";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_details);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setTitle("Details");
		reply = (ImageButton) findViewById(R.id.det_reply);
		retweet = (ImageButton) findViewById(R.id.det_retweet);
		flag = (ImageButton) findViewById(R.id.det_flag);
		
		mTwitter = new TwitterApp(DetailsActivity.this, twitter_consumer_key, twitter_secret_key);
		
		SharedPreferences sharedPrefs = getSharedPreferences(
		        getString(R.string.preference_file_key),MODE_PRIVATE);
		int state = sharedPrefs.getInt("gps", 0);
		if (state == 0){
			location = false;
		} else {
			location = true;
		}


		mTwitter.setListener(mTwLoginDialogListener);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			username = extras.getString("username");
			tweet = extras.getString("msg");
			lat = extras.getDouble("lat");
			lon = extras.getDouble("lon");
			TextView t_username = (TextView) findViewById(R.id.det_username);
			TextView t_tweet = (TextView) findViewById(R.id.det_msg);
			t_username.setText(username);
			t_tweet.setText(tweet);
			map = (ImageButton) findViewById(R.id.det_map);
			if (lat != 0.0 && lon != 0.0) {
				map.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						Intent intent = new Intent(Intent.ACTION_VIEW, Uri
								.parse("geo:" + lat + "," + lon + "?z=10&q="
										+ lat + "," + lon));
						startActivity(intent);
					}
				});
				map.setVisibility(View.VISIBLE);

			} else {
				map.setVisibility(View.GONE);
			}

			reply.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(DetailsActivity.this, ComposeActivity.class);
					intent.putExtra("username", username);
					startActivity(intent);

				}
			});

			retweet.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					postToTwitter(tweet);

				}
			});

			flag.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					SharedPreferences sharedPrefs = getSharedPreferences(
							getString(R.string.preference_file_key),MODE_PRIVATE);
					SharedPreferences.Editor editor = sharedPrefs.edit();
					editor.putInt("state", 1);
					editor.putString("ban", username);
					editor.commit();
					finish();
				}
			});
		}

	}

	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			String text = (msg.what == 0) ? "Posted to Twitter" : "Post to Twitter failed";

			Toast.makeText(DetailsActivity.this, text, Toast.LENGTH_SHORT).show();
		}
	};

	private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
		@Override
		public void onComplete(String value) {
			userTwitter    = mTwitter.getUsername();
			userTwitter    = (userTwitter.equals("")) ? "No Name" : userTwitter;

			Toast.makeText(DetailsActivity.this, "Connected to Twitter as " + userTwitter, Toast.LENGTH_LONG).show();
		}

		@Override
		public void onError(String value) {

			Toast.makeText(DetailsActivity.this, "Twitter connection failed", Toast.LENGTH_LONG).show();
		}
	};

	private void postToTwitter(final String review) {
		new Thread() {
			@Override
			public void run() {
				int what = 0;

				try {
					mTwitter.updateStatus(review);
				} catch (Exception e) {
					what = 1;
				}

				mHandler.sendMessage(mHandler.obtainMessage(what));
			}
		}.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.details, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		SharedPreferences sharedPrefs = getSharedPreferences(
		        getString(R.string.preference_file_key),MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPrefs.edit();
		switch (item.getItemId()) {
		case R.id.action_settings:
			intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
			break;
		case R.id.gps:
			if(location) {
				item.setIcon(R.drawable.gps_off);
				location = false;
				
				editor.putInt("gps", 0);
				editor.commit();
			} else {
				item.setIcon(R.drawable.gps_on);
				location = true;
				
				editor.putInt("gps", 1);
				editor.commit();
			}
			break;
		case R.id.poll:
			intent = new Intent(this, PollActivity.class);
			startActivity(intent);
			DetailsActivity.this.finish();
			break;
		case R.id.compose:
			intent = new Intent(this, ComposeActivity.class);
			startActivity(intent);
			DetailsActivity.this.finish();
			break;
		case android.R.id.home:
			DetailsActivity.this.finish();
			break;
		default:

		}
		return super.onOptionsItemSelected(item);
	}

}
